﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System.Xml.Serialization
Imports CMISObjectModel.BaseTypes

Namespace PropertyDefinitions
   Public MustInherit Class PropertyDefinition(Of ValueType)
      Inherits BaseDefinition
      Implements IPropertyDefinition
      Implements IXmlSerializable




      Public MustOverride ReadOnly Property PropertyTypeName() As String Implements IPropertyDefinition.PropertyTypeName

      Public Property Cardinality() As Enums.Cardinality = Enums.Cardinality.Single Implements IPropertyDefinition.Cardinality

      Public Property Updatability() As Enums.Updatability Implements IPropertyDefinition.Updatability

      Public Property Inherited() As Boolean = False Implements IPropertyDefinition.Inherited

      Public Property Required() As Boolean = False Implements IPropertyDefinition.Required

      Public Property Orderable() As Boolean Implements IPropertyDefinition.Orderable

      Public Property OpenChoice() As Boolean Implements IPropertyDefinition.OpenChoice

      Public Property DefaultValue As Properties.CmisProperty(Of ValueType)

      Public Property Choices() As PropertyDefinitions.Choices.CmisChoice(Of ValueType)()

      Public Property DefaultValueAsICMISProperty As Properties.ICMISProperty Implements IPropertyDefinition.DefaultValueAsICMISProperty
         Get
            Return DefaultValue
         End Get
         Set(value As Properties.ICMISProperty)
            DefaultValue = DirectCast(value, Properties.CmisProperty(Of ValueType))
         End Set
      End Property

      Public Property ChoicesAsObject As Object() Implements IPropertyDefinition.ChoicesAsObject
         Get
            Return Choices
         End Get
         Set(value As Object())
            Choices = DirectCast(value, PropertyDefinitions.Choices.CmisChoice(Of ValueType)())
         End Set
      End Property



      Public Sub New(id As String, localName As String, localNamespace As String, displayName As String, queryName As String, required As Boolean, inherited As Boolean, queryable As Boolean, orderable As Boolean, cardinality As Enums.Cardinality, updatability As Enums.Updatability)
         MyBase.New(id, localName, displayName, queryName)

         Me.Required = required
         Me.Inherited = inherited
         Me.Cardinality = cardinality
         Me.Updatability = updatability
         Me.Queryable = queryable
         Me.Orderable = orderable
         Me.LocalNamespace = localNamespace
      End Sub

      Public Sub New()

      End Sub



      Public Overridable Sub AddSpecialElements(writer As Xml.XmlWriter)

      End Sub



      Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
         Return Nothing
      End Function

      Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml

      End Sub

      Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
         Dim ns As String = CmisNamespaceConstants.Cmis
         Dim nsra As String = CmisNamespaceConstants.Cmisra
         Dim nsw3 As String = CmisNamespaceConstants.W3instance

         'Element order seems to be relevant!!!
         writer.WriteElementString("id", ns, Me.Id)
         writer.WriteElementString("localName", ns, Me.LocalName)
         writer.WriteElementString("localNamespace", ns, Me.LocalNamespace)
         writer.WriteElementString("displayName", ns, Me.DisplayName)
         writer.WriteElementString("queryName", ns, Me.QueryName)
         writer.WriteElementString("description", ns, Me.Description)
         writer.WriteElementString("propertyType", ns, Me.PropertyTypeName)
         writer.WriteElementString("cardinality", ns, Common.GetDataMemberValueFromEnum(Me.Cardinality, GetType(Enums.Cardinality)))
         writer.WriteElementString("updatability", ns, Common.GetDataMemberValueFromEnum(Me.Updatability, GetType(Enums.Updatability)))
         writer.WriteElementString("inherited", ns, CStr(Me.Inherited).ToLowerInvariant)
         writer.WriteElementString("required", ns, CStr(Me.Required).ToLowerInvariant)
         writer.WriteElementString("queryable", ns, Me.Queryable.ToString.ToLowerInvariant)
         writer.WriteElementString("orderable", ns, CStr(Me.Orderable).ToLowerInvariant)
         writer.WriteElementString("openChoice", ns, CStr(Me.OpenChoice).ToLowerInvariant)


         writer.WriteStartElement("defaultValue", ns)
         If DefaultValue IsNot Nothing Then
            DefaultValue.WriteXml(writer)
         End If
         writer.WriteEndElement() 'defaultValue

         AddSpecialElements(writer)

         If Choices IsNot Nothing Then
            For Each c As PropertyDefinitions.Choices.CmisChoice(Of ValueType) In Choices
               Dim serializer As New XmlSerializer(c.GetType)
               serializer.Serialize(writer, c)
            Next
         End If
      End Sub
   End Class
End Namespace